<?php

namespace App\Http\Requests\Api;

use Dingo\Api\Http\FormRequest;

class UsersRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        switch ( $this->method() ) {
            case 'POST':
                {
                    return true;

                    break;
                }
            case 'PUT':
                {
                    return true;

                    break;
                }
            default:
                {
                    break;
                }
        }
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        if ($this->isMethod('POST')) {

            $rules = [
                'email' => [
                    'email',
                    'unique:users,email',
                    'required',
                ],
                'phone' => [
                    'required_if:role,seller',
                    'regex:/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/',
                ],
                'password' => [
                    'required',
                ],
                'password_confirm' => [
                    'required',
                    'same:password',
                ],
                'role' => [
                    'required',
                    'in:customer,seller',
                ],
                'nick_name' => [
                    'required',
                ],
                'avatar' => [
                    'required',
                ],

                'age' => [
                    'integer',
                    'required_if:role,seller',
                ],
                'province' => [
                    'required_if:role,seller',
                    'exists:sys_province,name',
                ],
                'city' => [
                    'required_if:role,seller',
                    'exists:sys_city,name',
                ],
                'district' => [
                    'required_if:role,seller',
                    'exists:sys_district,name',
                ],
                'stature' => [
                    'required_if:role,seller',
                    'numeric',
                ],
                'weight' => [
                    'required_if:role,seller',
                    'numeric',
                ],
                'bust' => [
                    'required_if:role,seller',
                    'numeric',
                ],
                'waist' => [
                    'required_if:role,seller',
                    'numeric',
                ],
                'hips' => [
                    'required_if:role,seller',
                    'numeric',
                ],
                'video_cover' => [
                    'required_if:role,seller',
                ],
                'video' => [
                    'required_if:role,seller',
                ],
                'intro' => [
                    'size:150',
                ],
                'pictures' => [

                ],
            ];

            return $rules;
        }

        if ($this->isMethod('PUT')) {

            $rules = [
                'email' => [
                    'email',
                    'unique:users,email',
                ],
                'phone' => [
                    'regex:/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/',
                ],
                'password' => [

                ],
                'password_confirm' => [
                    'same:password',
                ],
                'role' => [
                    'in:customer,seller',
                ],
                'nick_name' => [

                ],
                'avatar' => [

                ],

                'age' => [
                    'integer',
                ],
                'province' => [
                    'exists:sys_province,name',
                ],
                'city' => [
                    'exists:sys_city,name',
                ],
                'district' => [
                    'exists:sys_district,name',
                ],
                'stature' => [
                    'numeric',
                ],
                'weight' => [
                    'numeric',
                ],
                'bust' => [
                    'numeric',
                ],
                'waist' => [
                    'numeric',
                ],
                'hips' => [
                    'numeric',
                ],
                'video_cover' => [

                ],
                'video' => [

                ],
                'intro' => [
                    'size:150',
                ],
                'pictures' => [

                ],
                'begin_business_hours' => [

                ],
                'end_business_hours' => [

                ],
                'begin_free_time' => [
                    'date_format:Y-m-d H:i',
                ],
                'end_free_time' => [
                    'date_format:Y-m-d H:i',
                ],
            ];

            return $rules;
        }
    }
}
